// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::command;
use std::fs;
mod database;

// 上传图片
#[command]
async fn upload_image(file: Vec<u8>, name: String, size: i64, type_: String, fbl: String, exif: String) -> Result<(), String> {
    // 检查文件是否已存在
    let mut final_name = name.clone();
    let mut counter = 1;
    while fs::metadata(format!("/home/hy/桌面/hy220/tauri-app/src-tauri/target/release/_up_/data/img{}", final_name)).is_ok() {
        let (file_name, ext) = name.rsplit_once('.').unwrap_or((&name, ""));
        final_name = format!("{}_{}.{}", file_name, counter, ext);
        counter += 1;
    }

    println!("Received image: {}", final_name);
    fs::write(format!("/home/hy/桌面/hy220/tauri-app/src-tauri/target/release/_up_/data/img/{}", final_name), file).map_err(|e| e.to_string())?;

    // 初始化数据库
    database::init_db().await.map_err(|e| e.to_string())?;
    // 插入信息
    database::insert_file(&final_name, size, &type_, &fbl, &exif).await.map_err(|e| e.to_string())?;
    Ok(())
}

// 删除图片
#[command]
async fn delete_image(name: String,id: i64) -> Result<(), String> {
    println!("Received image: {} {}", name,id);
    // 本地删除对应图片
    // fs::remove_file(format!("../data/img/{}", name)).map_err(|e| e.to_string())?;
    // 数据库中删除对应图片
    database::delete_img(&name,id).await.map_err(|e| e.to_string())?;
    // 22222
    Ok(())
}

// 获取所有图片
#[command]
async fn get_all_imgs() -> Result<Vec<(i64,String,String,i64,String,String,String)>, String> {
    database::get_all_imgs().await.map_err(|e| e.to_string())
}


// 删除相册
#[command]
async fn delete_album(id: i64) -> Result<(), String> {
    database::delete_album(id).await.map_err(|e| e.to_string())
}
// 创建相册
#[command]
async fn create_album(name: String,description: String) -> Result<(), String> {
    database::create_album().await.map_err(|e| e.to_string())?;
    database::insert_album(&name,&description).await.map_err(|e| e.to_string())
}
//修改相册
#[command]
async fn update_album(id: i64,name: String,description: String) -> Result<(), String> {
    database::update_album(id,&name,&description).await.map_err(|e| e.to_string())
}
// 获取所有相册
#[command]
async fn get_all_albums() -> Result<Vec<(i64,String,String,String)>, String> {
    let albums =
     database::get_all_albums().await.map_err(|e| e.to_string())?;

    //  println!("所有相册:{:?}", albums);
    Ok(albums)
}

// 获取相册里的图片
#[command]
async fn get_album_imgs(id: i64) ->Result<Vec<(i64,String,String,i64,String,String,String)>, String> {
    println!("相册id:{}", id);
    let imgs: Vec<(i64, String, String, i64, String, String, String)> = database::get_album_imgs(id).await.map_err(|e| e.to_string())?;
    println!("相册图片:{:?}", imgs);
    Ok(imgs)
}

// 添加图片到相册
#[command]
async fn add_img_to_album(album_id: i64,image_id: i64) -> Result<(), String> {
    println!("相册id:{}", album_id);
    println!("图片id:{}", image_id);

    //初始化相册图片表
    database::init_album_img().await.map_err(|e| e.to_string())?;
    
    database::add_img_to_album(album_id,image_id).await.map_err(|e| e.to_string())
}

// 从相册删除图片
#[command]
async fn remove_img_from_album(album_id: i64,image_id: i64) -> Result<(), String> {
    print!("从相册id:{}", album_id);
    println!("删除图片id:{}", image_id);
    database::remove_img_from_album(album_id,image_id).await.map_err(|e| e.to_string())       
}


#[command]
async fn check_permissions() -> Result<(), String> {
    let path = "/home/hy/桌面/hy220/tauri-app/src-tauri/target/release/_up_/data/img";
    if !std::path::Path::new(path).exists() {
        return Err("路径不存在".into());
    }
    Ok(())
}
fn main() {
  
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![
        upload_image, 
        delete_image,
         get_all_imgs, 
         create_album,
          get_all_albums,
          get_all_albums, 
          get_album_imgs,
          add_img_to_album,
          delete_album,
          update_album,
          remove_img_from_album,
          check_permissions
          ])
    .run(tauri::generate_context!())
    .expect("有错误");
}
